package pl.wroc.uni.ii.pastuszka.algorithms.evolutionary.rankers;

import pl.wroc.uni.ii.pastuszka.algorithms.evolutionary.Individual;

public class DeceptiveOneMax implements IndividualRanker {

  private int blockSize;

  public DeceptiveOneMax(int blockSize) {
    this.blockSize = blockSize;
  }

  protected int getBlockBonus(int onesInBlock) {
    return onesInBlock == 0 ? blockSize + 1 : 0;
  }

  @Override
  public float rank(Individual x) {

    int totalRank = 0;
    for (int i = 0; i < x.chromosome.length / blockSize; ++i) {
      totalRank += rankBlock(x, i);
    }

    return totalRank;
  }

  private int rankBlock(Individual x, int i) {
    int onesInBlock = 0;
    for (int j = 0; j < blockSize; ++j) {
      onesInBlock += x.chromosome[i * blockSize + j];
    }

    return onesInBlock + getBlockBonus(onesInBlock);
  }

}
